package com.squapsource.project_euler;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class Problem67 {

	/**
	 * @param args
	 * @throws URISyntaxException
	 * @throws IOException
	 */
	public static void main(String[] args) throws URISyntaxException,
			IOException {
		File triangleFile = new File(Problem67.class.getResource(
				"/Problem67.txt").toURI());

		String triangleString = FileUtils.readFileToString(triangleFile);
		int[][] triangle = getTriangleArray(triangleString);
		for (int i = 98; i>=0; i--)
		{
			for (int j=0; j<=i; j++)
			{
				if (triangle[i][j] + triangle[i+1][j] > triangle [i][j] + triangle[i+1][j+1])
				{
					triangle[i][j] = triangle[i][j] + triangle[i+1][j];
				}
				else
				{
					triangle[i][j] = triangle[i][j] + triangle[i+1][j+1];	
				}
			}
		}
		System.out.println(triangle[0][0]);

	}

	private static int[][] getTriangleArray(String gridString) {
		int[][] triangleArray = new int[100][100];
		List<String> lines = Arrays.asList(gridString.split("\n"));
		for (int i = 0; i < 100; i++) {
			List<String> cells = Arrays.asList(lines.get(i).split(" "));
			for (int j = 0; j < cells.size(); j++) {
				triangleArray[i][j] = Integer.parseInt(cells.get(j));
			}

		}
		return triangleArray;
	}

}
